import com.fxd.dao.IUserDao;
import com.fxd.doman.QueryVo;
import com.fxd.doman.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther fuxudong
 * @date 2020/12/17 - 17:11
 */
public class crudmabatistest {
    private InputStream in;
    private IUserDao userDao;
    private SqlSession sqlsession;

    @Before
    public void init() throws Exception {
        //1，读取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2, 创建SQLsessionfactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3, 使用工厂生产SQLsession对象
        sqlsession = factory.openSession();
        //4, 使用SQLsession创建dao接口的代理对象
        userDao = sqlsession.getMapper(IUserDao.class);
    }

    @After
    public void destroy() throws Exception {
        sqlsession.commit();
        sqlsession.close();
        in.close();

    }


    @Test
    public void findalltest() {

        //5, 使用代理对象执行方法
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }

    }

    @Test
    public void testsave() {
        //获取保存之后的ID
        User user = new User();
        user.setUsername("stone");
        user.setAddress("ch");
        user.setSex("g");
        user.setBirthday(new Date());
        System.out.println(user);
        userDao.saveUser(user);
        System.out.println(user);
    }
    @Test
    public void testupdate() {
        User user = new User();
        user.setId(48);
        user.setUsername("sto");
        user.setAddress("ch");
        user.setSex("g");
        user.setBirthday(new Date());

        userDao.updateUser(user);
    }
    @Test
    public void testdelete(){
        userDao.deleteUser(49);
    }

    @Test
    public void testfindone(){
        User user = userDao.findById(48);
        System.out.println(user);
    }
    @Test
    public void testfindbyname(){
       List<User> users = userDao.findByName("%王%");
       for(User user :users){
           System.out.println(user);
       }
    }
    @Test
    public void testfindtotol(){
        Integer sum = userDao.findtotol();
        System.out.println("总数是"+sum);

    }
    @Test
    public void testfindbyvo(){
        QueryVo queryVo = new QueryVo();
        User user = new User();
        user.setUsername("%王%");
        queryVo.setUser(user);
        List<User> users = userDao.findUserByVo(queryVo);
        for(User u :users){
            System.out.println(u);
        }
    }

    @Test
    public void testfindbycondition(){
        User user = new User();
        user.setUsername("老王");
        //user.setSex("女");
        List<User> users = userDao.findUserByCondition(user);
        for(User u :users){
            System.out.println(u);
        }
    }
    @Test
    public void findbyids() {
        QueryVo vo = new QueryVo();
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(41);
        list.add(42);
        list.add(46);
        vo.setIds(list);
        List<User> userInIds = userDao.findUserInIds(vo);
        for(User user : userInIds){
            System.out.println(user);
        }


    }


}
